// pages/classHomePage/integral/index/index.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    stuList: null,
    student_add_point: 0,
    people: null,
    rank_list: [],
    name_list: [],
    score_list: [],
    //自定义组件使用的数据
    card: [{
      title: {
        text: "积分",
        color: "#272635" //字体颜色
      },
      tag: {
        text: "1小时前"
      },
      header: {
        bgcolor: "#f5f5f5",  //#f5f5f5  #fbde35
        line: true
      }
    }],
    funList: {
      'funitems': [{
          "name": "PK",
          "url": "/pages/classHomePage/pk/index/index",
          "pic_url": "/assets/wx_pic/menu_pk@3x.png"
        },
        {
          "name": "盲盒",
          "url": "/pages/classHomePage/blindbox/index/index",
          "pic_url": "/assets/wx_pic/menu_mh@3x.png"
        },
        {
          "name": "签到",
          "url": "/pages/classHomePage/signIn/index/index",
          "pic_url": "/assets/wx_pic/menu_qd@3x.png"
        },
        {
          "name": "辩论会",
          "url": "/pages/classHomePage/debate/index/index",
          "pic_url": "/assets/wx_pic/menu_blh@3x.png"
        },
        {
          "name": "抢答",
          "url": "/pages/classHomePage/qiangda/index/index",
          "pic_url": "/assets/wx_pic/menu_qd_1@3x.png"
        },
        {
          "name": "讨论",
          "url": "/pages/classHomePage/discuss/index/index",
          "pic_url": "/assets/wx_pic/menu_tl@3x.png"
        },
        {
          "name": "成员分组",
          "url": "/pages/classHomePage/group/index/index",
          "pic_url": "/assets/wx_pic/menu_zyfz@3x.png"
        },
        {
          "name": "积分",
          "url": "/pages/classHomePage/integral/index/index",
          "pic_url": "/assets/wx_pic/menu_jf@3x.png"
        },
      ]
    },

  },

  /**
   * 生命周期函数--监听页面加载
   */

  addflower: function (e) {
    let _this = this
    // console.log('adde',e)
    let student_id = e.currentTarget.dataset['id']
    wx.showModal({
      editable: true, //显示输入框
      placeholderText: '输入积分', //显示输入框提示信息
      success: res => {
        if (res.confirm) { //点击了确认
          // console.log('用户输入的值', res.content)//用户输入的值
          this.setData({
            student_add_point: res.content
          })
          let data = {
            'code': wx.getStorageSync("myCode"),
            'course_name': app.globalData.banji_info['courseName'],
            'points': parseInt(_this.data.student_add_point),
            'student_id': student_id,
            'teacher_account': wx.getStorageSync("account"),
          }
          app.formPost('/add_student_pounts?', data).then(res => {
            console.log('正在给学生加分：', res);
            _this.onLoad()
          })
        } else {
          console.log('用户点击了取消')
        }
      }
    })
  },

  onLoad(options) {
    let _this = this
    _this.setData({
      people: wx.getStorageSync("people")
    })
    return new Promise(function (resolve, reject) {
      if (_this.data.people == 'teacher') {
        let course_name = app.globalData.banji_info['courseName']
        app.formGet('/get_points_sort?course_name=' + course_name).then(res => {
          console.log('老师获取分数列表:', res)
          res = res.sort((a,b)=>{
            return b.points-a.points
          })
          // console.log("1111"+res[0]['points'])
          let tag = {}
          let name = {}
          let rank = {}
          let new_name_list = []
          let new_score_list = []
          let new_rank_list = []

          for (let i = 0; i < res.length; i++) {
            name = {
              'text':String(res[i]['student_name'])
            }
            tag = {
              'text': String(res[i]['points'])
            }
            rank = {
              'text': String("第"+(i+1)+"名")
            }
            new_name_list.push(name)
            new_score_list.push(tag)
            new_rank_list.push(rank)
          }

          _this.setData({
            stuList: res,
            name_list: new_name_list,
            score_list: new_score_list,
            rank_list: new_rank_list
          })
        })
      }
      if (_this.data.people == 'student') {
        app.formPost('/get_all_class_by_student_id?student_id=' + app.globalData.student_info['id'], {}).then(res => {
          let course_name = res[0]['className']
          let tag = {}
          let name = {}
          let rank = {}
          let new_name_list = []
          let new_score_list = []
          let new_rank_list = []
          app.formGet('/get_points_sort?course_name=' + course_name).then(res => {
            console.log('学生获取分数列表:',res)
            for (let i = 0; i < res.length; i++) {
              name = {
                'text':String(res[i]['student_name'])
              }
              tag = {
                'text': String(res[i]['points'])
              }
              rank = {
                'text': String("第"+(i+1)+"名")
              }
              new_name_list.push(name)
              new_score_list.push(tag)
              new_rank_list.push(rank)
            }
            _this.setData({
                stuList: res,
                name_list: new_name_list,
                score_list: new_score_list,
                rank_list: new_rank_list
            })
          })
        })
      }
    })
  },

  click_item: function (e) {
    if (e.currentTarget.dataset.url != '') {
      wx.navigateBack()
      wx.navigateTo({
        url: e.currentTarget.dataset.url,
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      people: wx.getStorageSync("people")
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})